package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    //敏感词查询
    @Override
    public ResponseResult list(SensitiveDto dto) {
        //1:校验参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();//校验分页的页码和条数

        //2.name 模糊查询 分页
        Page pageParam=new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        //isnotblank=name!=""&&name!=null isnotempty=name!=""
        if (StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        IPage reslut=page(pageParam,queryWrapper);

        //3.结果封装到ResponseResult
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)reslut.getTotal());
        responseResult.setData(reslut.getRecords());
        return responseResult;
    }
    //添加
    @Override
    public ResponseResult insertSensitive(AdSensitive adSensitive) {
        //1.校验参数
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //时间
        adSensitive.setCreatedTime(new Date());
        //添加
        boolean b = this.save(adSensitive);
        if (b){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }
    //修改
    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        //1.校验参数
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改
        boolean b = this.updateById(adSensitive);
        if (b){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    //删除
    @Override
    public ResponseResult deleteById(Integer id) {
        //1:校验参数
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询id
        AdSensitive adSensitive = this.getById(id);
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //删除id
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
